package de.stefanocke.roo.wicket.base;

import static org.springframework.roo.model.JavaType.VOID_OBJECT;

import java.util.logging.Logger;

import org.springframework.roo.classpath.PhysicalTypeMetadata;
import org.springframework.roo.classpath.details.annotations.populator.AutoPopulate;
import org.springframework.roo.model.JavaType;
import org.springframework.roo.support.logging.HandlerUtils;

public abstract class ModelClassAwareAnnotationValuesBase extends AnnotationValuesBase implements ModelClassAware,
		AutoWireSettings, BeanPropertiesClassAware {

	protected static final Logger logger = HandlerUtils.getLogger(ModelClassAwareAnnotationValuesBase.class);

	@AutoPopulate
	private JavaType modelClass;

	/**
	 * The governor class representing the bean properties metadata. For
	 * disambiguation, if there are multiple ones for one entity.
	 */
	@AutoPopulate(value = "properties")
	private JavaType beanPropertiesClass;

	/**
	 * by default, all wicket metadata is target for "autowiring". That is, it
	 * will be found be other metadata, without explicitely refering to it.
	 */
	@AutoPopulate
	private boolean autowire = true;

	protected ModelClassAwareAnnotationValuesBase(PhysicalTypeMetadata governorPhysicalTypeMetadata,
			Class<?> annotationType) {
		this(governorPhysicalTypeMetadata, annotationType, false);

	}

	protected ModelClassAwareAnnotationValuesBase(PhysicalTypeMetadata governorPhysicalTypeMetadata,
			Class<?> annotationType, boolean subclassWillCallPopulate) {
		super(governorPhysicalTypeMetadata, annotationType);
		if (!subclassWillCallPopulate) {
			populate();
		}
	}

	@Override
	public JavaType getModelClass() {
		return modelClass;
	}

	@Override
	public boolean isAutowireCandidate() {
		return autowire;
	}

	@Override
	//TODO: This is not the proper place. Create a subclass BeanPropertiesAwareAnnotationValueBase
	public JavaType getBeanPropertiesClass() {

		if (VOID_OBJECT.equals(beanPropertiesClass) || JavaType.VOID_PRIMITIVE.equals(beanPropertiesClass)) {
			return null;
		}
		return beanPropertiesClass;
	}

}
